<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增菜单' : '编辑菜单'" append-to-body width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="菜单图标">
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()">
              <IconSelect ref="iconSelect" @selected="selected" />
              <el-input slot="reference" v-model="form.icon" style="width: 460px;" placeholder="点击选择图标" readonly>
                <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon" style="height: 32px;width: 16px;" />
                <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
              </el-input>
            </el-popover>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
            <el-input v-model="form.name" placeholder="菜单名称" style="width: 460px;" clearable/>
        </el-form-item>
        <el-form-item label="菜单排序" prop="sort">
            <el-input-number v-model.number="form.sort" :min="0" :max="999" controls-position="right" style="width: 460px;"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
            <el-radio v-model="form.isDisable" :label="0">是</el-radio>
            <el-radio v-model="form.isDisable" :label="1" >否</el-radio>
        </el-form-item>
        <el-form-item label="链接地址" prop="path">
            <el-input v-model="form.path" placeholder="菜单路径" style="width: 440px;" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" clearable/>
            <el-tooltip class="tooltip" placement="top" effect="light">
                <div slot="content">
                      <p>链接地址填写注意:</p>
                      <p>1:顶级类目的链接地址一定要以'/'开始,如：'/system'</p>
                      <p>2.非顶级类目为链接地址省略'/'开始，如：'permission'</p>
                </div>
                <i class="el-icon-question"></i>
            </el-tooltip>
        </el-form-item>

        <el-form-item label="组件路径" prop="component">
            <el-input v-model="form.component" placeholder="组件路径" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" style="width: 440px;" clearable/>
            <el-tooltip class="tooltip" placement="top" effect="light">
                <div slot="content">
                      <p>组件路径填写注意:</p>
                      <p>1:顶级类目的组件路径为统一'Layout'</p>
                      <p>2.非顶级类目为文件夹路径，如：system/permission/index</p>
                </div>
                <i class="el-icon-question"></i>
            </el-tooltip>
        </el-form-item>
        

        <el-form-item label="上级类目" prop="pid">
            <treeselect v-model="form.pid" :options="menus" style="width: 460px;" placeholder="选择上级类目" />
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { add, edit, getMenusTree } from '@/api/menu'
import IconSelect from '@/components/IconSelect'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { validateEmpty } from '@/utils/validate'
export default {
  components: { Treeselect, IconSelect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
        loading: false, dialog: false, menus: [],
        form: { 
            icon: '',name: '', sort: 1, path: '', isDisable:0, component: '',  pid: -2, permissionGrade: '1',
        },
        rules: {
            icon: [
              { required: true, message: '请选择图标', trigger: 'blur' }
            ],
            name: [
              { required: true, message: '请输入菜单名称', trigger: 'blur' },
              {validator:validateEmpty,trigger: 'blur',}
            ],
            sort: [
              { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
            ],
            component: [
              { required: true, message: '请输入组件路径', trigger: 'blur' }
            ],
            path: [
              { required: true, message: '请输入链接地址', trigger: 'blur' }
            ],
            pid: [
              { required: true, message: '请选择上级类目', trigger: 'blur' }
            ]
        }
    }
  },
  methods: {
    cancel() {
        this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
        // if (this.form.pid == -2) {
        //     this.form = {
        //         icon: this.form.icon,
        //         name: this.form.name, 
        //         sort: this.form.icon,
        //         path: '/'+this.form.path,
        //         isDisable: this.form.isDisable,
        //         component:  this.form.component,
        //         pid:  this.form.pid,
        //         permissionGrade: this.form.permissionGrade,
        //     }
        // }
        add(this.form).then(res => {
            if (res.code == 2000) {
              this.resetForm()
              this.$notify({
                  title: '添加成功',
                  type: 'success',
                  duration: 2500
              })
              this.loading = false
              this.$parent.init()
            }
         }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
        })
    },
    doEdit() {
        add(this.form).then(res => {
          if (res.code == 2000) {
            this.resetForm()
            this.$notify({
                title: '修改成功',
                type: 'success',
                duration: 2500
            })
            this.loading = false
            this.$parent.init()
          }
        }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
        })
    },
    resetForm() {
        this.dialog = false
        this.$refs['form'].resetFields()
        this.form = { icon: '',name: '', sort: 1, path: '', isDisable:'0', component: '',  pid: -2, permissionGrade: '1', }
    },
    selected(name) {
        this.form.icon = name
    },
    getMenus() {
        getMenusTree().then(res => {
          if (res.code == 2000) {
            this.menus = []
            const menu = { id: -2, label: '顶级类目', children: [] }
            menu.children = res.data
            this.menus.push(menu)
          }
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
